package cn.fl.atc.service.impl;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.atc.service.IAtcRlPsnService;
import cn.fl.atc.transform.AtcRlPsnVOTransform;
import cn.fl.atc.vo.AtcRlPsnVO;
import cn.fl.framework.base.context.CurrentThreadContext;
import fl.spl.facade.atc.api.IAtcRlPsnApiService;
import fl.spl.facade.atc.condition.AtcRlPsnQueryCondition;
import fl.spl.facade.atc.constant.AtcBscInfoConstant;
import fl.spl.facade.atc.dto.AtcRlPsnDTO;
import fl.spl.facade.framework.constant.DictionaryConstant;

/**
 * @Description: 汽贸城关联人员信息表 客户端服务层接口实现
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class AtcRlPsnServiceImpl implements IAtcRlPsnService {

	/**
	 * 日志
	 */
	private static final Logger logger = LoggerFactory.getLogger(AtcRlPsnServiceImpl.class);

	/**
	 * TODO 自定义 API 暴露服务
	 */
	@Autowired
	private IAtcRlPsnApiService atcRlPsnApiService;
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;
	@Override
	public AtcRlPsnVO selectById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "selectById", "主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		AtcRlPsnQueryCondition qc = new AtcRlPsnQueryCondition();
		qc.setId(id);
		qc.setIsDel(AtcBscInfoConstant.IS_STATUS_0);
		List<AtcRlPsnVO> searchList = searchList(qc);
		return searchList.get(0);
	}

	@Override
	public Long add(AtcRlPsnVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "add", "新增对象不能为空!", CurrentThreadContext.getCurrentUserName());
			return null;
		}
		//return this.atcRlPsnApiService.add(AtcRlPsnVOTransform.toDTO(entity));
		//TODO 业务逻辑
		return null;
	}

	@Override
	public boolean removeById(Long id) throws BaseException{
		if(StringUtils.isEmpty(id)){
			BaseLogUtils.info(logger, "removeById", "删除主键参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		//return this.atcRlPsnApiService.removeById(id);
		//TODO 业务逻辑
		return false;
	}
	
	/**
     * 删除
     * @param ids
     * @return
     */
    public boolean removeByIds(List<Long> ids)throws BaseException{
    	if(CollectionUtils.isEmpty(ids)){
    		BaseLogUtils.info(logger, "removeByIds", "删除主键参数集合不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		};
		return false;
    }

	@Override
	public boolean modify(AtcRlPsnVO entity) throws BaseException{
		if(null == entity){
			BaseLogUtils.info(logger, "modify", "修改对象参数不能为空!", CurrentThreadContext.getCurrentUserName());
			return false;
		}
		return atcRlPsnApiService.modifyAtcRlPsn(AtcRlPsnVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException{
		if(null == page)page = new Pager();
		//return this.atcRlPsnApiService.searchListPage(page);
		//TODO 业务逻辑
		return null;
	}

	@Override
	public List<AtcRlPsnVO> searchList(AtcRlPsnQueryCondition qc) {
		List<AtcRlPsnDTO> searchAtcRlPsnList = atcRlPsnApiService.searchAtcRlPsnList(qc);
		List<AtcRlPsnVO> voList = AtcRlPsnVOTransform.toVOList(searchAtcRlPsnList);
		setDicInfo(voList);
		return voList;
	}
	/**
	 * 设置主要人员基本信息中关于字典项信息
	 * @param listDTO
	 */
	private void setDicInfo(List<AtcRlPsnVO> listVO) {
		if(CollectionUtils.isEmpty(listVO))return;
		String sysCd = CurrentThreadContext.getCurrentSysCd();
		String orgCd = CurrentThreadContext.getCurrentOrgCd();
		Map<String, String> certTypCdMap = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, DictionaryConstant.CODE_GRP_CERT_TYP_CD);
		Map<String, String> rlTypeCdMap = this.sysDictionaryApiService.searchGroupToMap(sysCd, orgCd, DictionaryConstant.CODE_RL_TYP_CD);
		for (AtcRlPsnVO vo : listVO) {
			//证件类型
			if(certTypCdMap.containsKey(vo.getCertTypCd())){
				vo.setCertTypCdNm(certTypCdMap.get(vo.getCertTypCd()));
			}
			
			//人员类型
			if(rlTypeCdMap.containsKey(vo.getRlTypCd())){
				vo.setRlTypCdNm(rlTypeCdMap.get(vo.getRlTypCd()));
			}
		}
	}
}
